/**
 * 
 */
package com.edocbase.desktop.swt.widgets;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

/**
 * @author X61
 *
 */
class ExplorerContentProvider implements /*ILazyTreeContentProvider,ILazyTreePathContentProvider,*/ITreeContentProvider {
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger
		.getLogger(ExplorerContentProvider.class);
	private TreeViewer treeViewer = null;
	private FileNode root = null;
	@Override
	public Object getParent(Object element) {
		FileNode node =(FileNode)element;
		if (logger.isDebugEnabled()) {
			logger.debug("" + node);
		}
		return node.getParent();
	}

//	@Override
//	public void updateChildCount(Object element, int currentChildCount) {
//		FileNode node =(FileNode)element;
//		int childCount =0;
////		if (element == root){
////			childCount = 1;
////		}else{
//			childCount = node.getChildCount();
////		}
//		
////		if (currentChildCount == 0){
////			return;
////		}
//		if (logger.isDebugEnabled()) {
//			logger.debug("" + node + " childCount " + childCount);
//		}
//		treeViewer.setChildCount(node, childCount);
//	}
//
//	@Override
//	public void updateElement(Object parent, int index) {
//		FileNode node =(FileNode)parent;
//		if (node == root){
//			treeViewer.replace(parent, index, node);
//		}
//		FileNode child = node.getChildNode(index);
//		if (child == null){
//			return;
//		}
//		if (logger.isDebugEnabled()) {
//			logger.debug("node is root " + node + " index " + index + " child " + child);
//		}
//		treeViewer.replace(parent, index, child);
//		if (child.hasChildren()){
//			//treeViewer.setChildCount(child, 1);
//			treeViewer.setHasChildren(child, true);
//		}
//	}

	@Override
	public void dispose() {
		
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		this.treeViewer = (TreeViewer)viewer;
		this.root = (FileNode) newInput;
		//this.treeViewer.setChildCount(this.root, this.root.getChildCount());
		if (logger.isDebugEnabled()) {
			logger.debug(""+this.root);
		}
	}
//
//	@Override
//	public TreePath[] getParents(Object element) {
//		FileNode node = (FileNode)element;
//		List<TreePath> pathList =new ArrayList<TreePath>();
//		while (node.getParent() != null){
//			TreePath path = new TreePath(new FileNode[]{node});
//			pathList.add(path);
//		}
//		TreePath[] paths = new TreePath[pathList.size()];
//		paths = pathList.toArray(paths);
//		return paths;
//	}
//
//	@Override
//	public void updateChildCount(TreePath treePath, int currentChildCount) {
//		FileNode node = null;
//		if (treePath.getSegmentCount()==0){
//			node = root;
//		}else{
//			node = (FileNode) treePath.getLastSegment();
//		}
//		int childCount = node.getChildCount();
//		treeViewer.setChildCount(treePath, childCount);
//	}
//
//	@Override
//	public void updateElement(TreePath parentPath, int index) {
//		FileNode node = parentPath.getSegmentCount() ==0?root:(FileNode) parentPath.getLastSegment();
//		TreePath treePath = parentPath.createChildPath(node.getChildNode(index));
//		treeViewer.replace(parentPath, index, node);
//		updateHasChildren(treePath);
//	}
//
//	@Override
//	public void updateHasChildren(TreePath path) {
//		FileNode node = (FileNode) path.getLastSegment();
//		if (node.hasChildren()){
//			treeViewer.setHasChildren(path, true);
//		}
//	}

	@Override
	public Object[] getChildren(Object parentElement) {
		FileNode node =(FileNode)parentElement;
		return node.getChildren();
	}

	@Override
	public boolean hasChildren(Object element) {
		FileNode node =(FileNode)element;
		return node.hasChildren();
	}

	@Override
	public Object[] getElements(Object inputElement) {
		if (logger.isDebugEnabled()) {
			logger.debug("" + inputElement);
		}
//		if (inputElement == root){
//			return new FileNode[]{root};
//		}
		FileNode node =(FileNode)inputElement;
		return node.getChildren();
	}


}
